<template>
	<view class="container">
		<view class="user-list x-bc jui_flex jui_flex_justify_between jui_flex_items_center">
			<text class="list-name">头像</text>
			<view class="x-f jui_flex jui_flex_items_center" @tap="onChooseImg">
				<image class="avatar" :src="userData.avatar" mode=""></image>
				<image src="/static/imgs/home/right.png" style="height: 24rpx;width: 14rpx; margin-left: 14rpx;" mode=""></image>
			</view>
		</view>
		<view class="user-list x-bc jui_flex jui_flex_justify_between jui_flex_items_center">
			<text class="list-name">昵称</text>
			<view class="x-f jui_flex jui_flex_items_center" >
				<input class="list-val" v-model="userData.nickname" />
				<text class="cuIcon-right"></text>
			</view>
		</view>
		<view class="user-list x-bc jui_flex jui_flex_justify_between jui_flex_items_center">
			<text class="list-name">等级</text>
			<view class="x-f jui_flex jui_flex_items_center">
				<text class="list-val">{{ userData.level_name }}</text>
			</view>
		</view>
		<view class="user-list x-bc jui_flex jui_flex_justify_between jui_flex_items_center">
			<text class="list-name">手机号</text>
			<view class="x-f jui_flex jui_flex_items_center">
				<text class="list-val">{{ userData.mobile }}</text>
				<!-- <text class="cuIcon-right"></text> -->
			</view>
		</view>
		<!-- <view class="user-list x-bc">
			<text class="list-name">ID</text>
			<view class="x-f">
				<text class="list-val">{{ userData.id }}</text>
				
			</view>
		</view> -->
		<view class="user-list x-bc jui_flex jui_flex_justify_between jui_flex_items_center">
			<text class="list-name">邀请码</text>
			<view class="x-f jui_flex jui_flex_items_center">
				<text class="list-val">{{ userData.invite_code }}</text>
			</view>
		</view>
		<view class="user-list x-bc jui_flex jui_flex_justify_between jui_flex_items_center" v-if="userData.t_name">
			<text class="list-name">邀请人</text>
			<view class="x-f">
				<text class="list-val">{{ userData.t_name }}</text>
			</view>
		</view>
		<view class="user-list x-bc jui_flex jui_flex_justify_between jui_flex_items_center" v-if="userData.u_tid">
			<text class="list-name">邀请人ID</text>
			<view class="x-f">
				<text class="list-val">{{ userData.u_tid}}</text>
			</view>
		</view>
		<view class="btn-box flex align-center justify-center"><button class="cu-btn confirem-btn" @tap="editUserInfo">保存</button></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userData: {},
				avatar:"",
				src1:'',
				fromToken:''
			};
		},
		onLoad() {
			// this.userData = uni.getStorageSync('g_user');
		},
		onShow() {
			
			this.getFromToken();
		},
		methods: {
			getUser() {
				var that = this;
				let data = {
					FromToken: this.fromToken,
					uid: uni.getStorageSync('loginInfo').id,
				}
				this.$api.Center(data).then(res => {
					this.userData = res.data.user_info
					uni.setStorage({
						key:'userInfo',
						data:res.data
					})
					
				});
			},
			getFromToken() {
				this.$api.getFromToken({
					data: 1
				}).then(res => {
					if (res.code == 1) {
						this.fromToken = res.FromToken;
						this.getUser();
					}
				})
			},
			// 选择日期
			onDateChange(e) {
				this.userData.birthday = e.detail.value;
			},
			// 选择图片
			onChooseImg() {
				let _this = this
				uni.chooseImage({
					count: 10,
					sourceType: ['album'],
					success: function(res) {
						uni.getImageInfo({
							src: res.tempFilePaths[0],
							success: function(image) {
								// console.log(image);
								// console.log(res.tempFilePaths[0]);
								_this.userData.avatar = (res.tempFilePaths[0]);
								// console.log(_this.userData.avatar);
								_this.avatar == 1
								uni.uploadFile({
									url:_this.global.baseUrl +  'members/task/UploadImages',
									filePath: res.tempFilePaths[0],
									name: 'img',
									header: {
										token: uni.getStorageSync('token')
									},
									success: uploadFileRes => {
										// let res = JSON.parse(uploadFileRes.data);
										// console.log(uploadFileRes);
										// console.log(JSON.parse(uploadFileRes.data))	
										_this.$msg(res.msg)
										let res1 = JSON.parse(uploadFileRes.data);
										_this.src1 = (res1.id);
										
									}
								});
							},
						});
					}
				});
			},
			editUserInfo() {
				let that = this;
				var data = {
					avatar:that.src1,
					nickname: that.userData.nickname,
				}
				that.$api.SetUserInfo(data).then(res => {
					if (res.code === 1) {
						that.$msg('修改成功');
						setTimeout(function(){
							uni.navigateBack();
						},1000)
					}
				});
				
			},
		}
	};
</script>

<style lang="scss">
	.user-list {
		background: #fff;
		height: 100rpx;
		border-bottom: 1rpx solid #f6f6f6;
		padding: 0 20rpx;

		.list-name {
			font-size: 28rpx;
		}

		.avatar {
			width: 67rpx;
			height: 67rpx;
			border-radius: 50%;
			// background: #ccc;
		}

		.cuIcon-right {
			margin-left: 25rpx;
		}

		.list-val {
			color: #999;
			font-size: 28rpxc;
			text-align: right;
		}
	}

	.btn-box {
		margin-top: 60rpx;
	
		.confirem-btn {
			width: 710rpx;
			height: 80rpx;
			background: $zhuse;
			// background: linear-gradient(90deg, rgba(233, 180, 97, 1), rgba(238, 204, 137, 1));
			border: 1rpx solid rgba(239, 58, 58, 0.22);
			border-radius: 40rpx;
			font-size: 30rpx;
			color: rgba(#fff, 0.9);
		}
	}
</style>
